package com.example.tvmenu;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.tvmenu.adapter.GridAdapter;
import com.example.tvmenu.adapter.MenuAdapter;
import com.example.tvmenu.adapter.MenuSonAdapter;
import com.example.tvmenu.data.ItemDatas;
import com.example.tvmenu.foucs.AbsFocusBorder;
import com.example.tvmenu.foucs.FocusBorder;
import com.owen.tvrecyclerview.widget.TvRecyclerView;
import com.owen.tvrecyclerview.widget.V7GridLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends AppCompatActivity {
    @BindView(R.id.list_v7_hsc)
    TvRecyclerView tvMenuBig;
    @BindView(R.id.list_hsc)
    TvRecyclerView tvMenuSmall;
    @BindView(R.id.list_hsc_image)
    TvRecyclerView tvImage;
    private MenuAdapter menuBigAdapter;
    private MenuSonAdapter menuSmallAdapter;
    private GridAdapter imageAdapter;
    private List<String[]> rightMenus;
    protected FocusBorder mFocusBorder;
    private boolean isFirstShow = true;
    private boolean sencondToLeft;
    private boolean sencondToRight;
    private boolean menuToRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_list_hsc);
        ButterKnife.bind(this);
        initData();
        initFocusDrawableBorder(AbsFocusBorder.Mode.NOLL);

        // 左侧主菜单

        tvMenuBig.setSpacingWithMargins(30, 0);
        menuBigAdapter = new MenuAdapter(this, true);
        menuBigAdapter.setDatas(ItemDatas.getLeftMenu(5));
        tvMenuBig.setAdapter(menuBigAdapter);
//        tvMenuBig.setSelectedItemAtCentered(true);
        tvMenuBig.setSelection(0);
//        tvMenuBig.scrollToPositionWithOffset(0, 0, true);
        //右侧子菜单，默认取0
        tvMenuSmall.setSpacingWithMargins(40, 0);
        menuSmallAdapter = new MenuSonAdapter(this, false);
        tvMenuSmall.setSelectedItemAtCentered(true);
        //右侧图片

        //图片数据
        V7GridLayoutManager gridLayoutManager = new V7GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        tvImage.setLayoutManager(gridLayoutManager);
        imageAdapter = new GridAdapter(this);
        imageAdapter.setDatas(ItemDatas.getImageDatas());
        tvImage.setSpacingWithMargins(50, 20);
        tvImage.setAdapter(imageAdapter);
        setListener();
    }

    private void initData() {
        rightMenus = new ArrayList<>();
        rightMenus.add(ItemDatas.familys);
        rightMenus.add(ItemDatas.ships);
        rightMenus.add(ItemDatas.journeys);
        rightMenus.add(ItemDatas.holidays);
        rightMenus.add(ItemDatas.memorys);
    }

    private int currentMenuPostion;

    private void setListener() {


        //焦点监听
        mFocusBorder.boundGlobalFocusListener(new FocusBorder.OnFocusCallback() {
            @Nullable
            @Override
            public FocusBorder.Options onFocus(View oldFocus, View newFocus) {
                return focus(oldFocus, newFocus);
            }
        });


        tvMenuBig.setOnItemListener(new TvRecyclerView.OnItemListener() {


            @Override
            public void onItemPreSelected(TvRecyclerView parent, View itemView, int position) {
                if (isFirstShow) {
                    itemView.setBackgroundResource(R.drawable.item_background_activited);
                    ((TextView) itemView.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.color_ffac00));
                    isFirstShow = false;
                } else if (menuToRight) {
                    itemView.setBackgroundResource(R.drawable.item_background_activited);
                    ((TextView) itemView.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.color_ffac00));
                    menuToRight = false;
                } else {

                    itemView.setBackgroundResource(R.drawable.item_background_defult);
                }
                DebugLog.d(" kun 父222222-------onItemPreSelected  " + " postion " + position);
            }

            @Override
            public void onItemSelected(TvRecyclerView parent, View itemView, int position) {
                DebugLog.d(" kun 父222222-------onItemSelected  " + " postion " + position);
                itemView.setBackgroundResource(R.drawable.item_background_foucs);
                ((TextView) itemView.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.black_opaque));
                currentMenuPostion = position;
                if (isFirstShow) {
                    menuSmallAdapter.setDatas(ItemDatas.getrightMenu(rightMenus.get(0)));
                    tvMenuSmall.setAdapter(menuSmallAdapter);
                    tvMenuSmall.scrollToPositionWithOffset(0, 0, true);

                } else {
                    menuSmallAdapter.updateDatas(ItemDatas.getrightMenu(rightMenus.get(position)));
                }

            }

            @Override
            public void onItemClick(TvRecyclerView parent, View itemView, int position) {
                DebugLog.d(" kun 父222222-------onItemClick  ");
            }
        });
        tvMenuSmall.setOnItemListener(new TvRecyclerView.OnItemListener() {
            @Override
            public void onItemPreSelected(TvRecyclerView parent, View itemView, int position) {
                itemView.setBackgroundResource(R.drawable.item_background_activited);
                ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
                layoutParams.width = getResources().getDimensionPixelOffset(R.dimen.x_tv_45);
                itemView.setLayoutParams(layoutParams);
                DebugLog.d(" kun 子-11111111-------onItemPreSelected  " + " postion " + position);
                if (sencondToLeft) {
                    itemView.setBackgroundResource(R.drawable.item_background_defult);
                    sencondToLeft = false;
                } else if (sencondToRight) {

                    sencondToRight = false;
                } else {
                    itemView.setBackgroundResource(R.drawable.item_background_defult);
                }
            }

            @Override
            public void onItemSelected(TvRecyclerView parent, View itemView, int position) {
                DebugLog.d(" kun 子-11111111-------onItemSelected  " + " postion " + position);
                itemView.setBackgroundResource(R.drawable.item_background_foucs);
                ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
                layoutParams.width = getResources().getDimensionPixelOffset(R.dimen.x_tv_50);
                itemView.setLayoutParams(layoutParams);
            }

            @Override
            public void onItemClick(TvRecyclerView parent, View itemView, int position) {
                DebugLog.d(" kun子-11111111-------onItemClick  ");
            }
        });
        tvMenuSmall.setOnInBorderKeyEventListener(new TvRecyclerView.OnInBorderKeyEventListener() {
            @Override
            public boolean onInBorderKeyEvent(int direction, View focused) {
                switch (direction) {
                    case View.FOCUS_RIGHT:
                        sencondToRight = true;
                        break;
                    case View.FOCUS_LEFT:
//
                        sencondToLeft = true;
                        tvMenuBig.scrollToPositionWithOffset(currentMenuPostion, 0, true);
                        break;

                }
                return false;
            }
        });
        tvMenuBig.setOnInBorderKeyEventListener(new TvRecyclerView.OnInBorderKeyEventListener() {
            @Override
            public boolean onInBorderKeyEvent(int direction, View focused) {
                switch (direction) {
                    case View.FOCUS_RIGHT:
                        menuToRight = true;
                        tvMenuSmall.scrollToPositionWithOffset(0, 0, true);
                        break;

                }
                return false;
            }
        });

    }

    /**
     * 焦点位移监听
     *
     * @param oldFocus 老view
     * @param newFocus 新view
     * @return
     */
    protected FocusBorder.Options focus(View oldFocus, View newFocus) {
        switch (newFocus.getId()) {
            case R.id.fl_round:
                return createFocusBorder(0);
        }
        mFocusBorder.setVisible(false);
        return null;
    }

    /**
     * 初始化图片焦点
     *
     * @param mode 动画模式
     */
    public void initFocusDrawableBorder(AbsFocusBorder.Mode mode) {
        mFocusBorder = new FocusBorder.Builder().asDrawable()
                //阴影宽度(方法shadowWidth(18f)也可以设置阴影宽度)
                //阴影颜色
                .borderDrawableRes(R.drawable.focus)
                //标题相关配置
                /*   .titleBackgroundRes(R.drawable.shape_item_title_bg)
                   .titleMarginBottomAutoAlignBorder()
                   .titleMargin(10, 0, 10, 4)
   //                .titlePadding(10)
                   .titleWidth(ViewGroup.LayoutParams.MATCH_PARENT)
                   .titleTextColor(Color.LTGRAY)
                   .titleTextSize(24)*/
                //边框动画模式AbsFocusBorder.Mode.SEQUENTIALLY
                .animMode(mode)
                .build(this);

    }

    /**
     * @param radius 圆角
     * @return 焦点动画框
     */
    public FocusBorder.Options createFocusBorder(int radius) {
        float scale = 1.10f;
        return FocusBorder.OptionsFactory.get(scale, scale, radius);
    }

}
